# -*- coding: utf-8 -*-
# @Time    : 2024/8/29 0:58
# @Author  : Alvin
# @File    : forms.py
from flask_wtf.file import FileAllowed
from wtforms import IntegerField, StringField, FileField, TextAreaField
from wtforms.validators import DataRequired, length, URL, Optional, ValidationError

from app.libs.enums import ClientTypeEnum
from app.validators.base import BaseForm


class ClientForm(BaseForm):
    account = StringField(validators=[DataRequired(message='不允许为空'), length(
        min=5, max=32
    )])
    secret = StringField()
    type = IntegerField(validators=[DataRequired()])

    def validate_type(self, value):
        try:
            client = ClientTypeEnum(value.data)
        except ValueError as e:
            raise ValidationError(f'{e}')
        self.type.data = client


class UserMinaForm(ClientForm):
    # 字段名和前端保持一致
    avatarUrl = StringField('URL', [Optional(),
                                   URL(message='无效的 URL')
                                   ])
    nickName = StringField(validators=[DataRequired(),
                                        length(min=2, max=22)])


class TokenForm(BaseForm):
    token = StringField(validators=[DataRequired()])


class UploadForm(BaseForm):
    # 文件自带文件名
    # file_name = StringField()
    # file_type = IntegerField()
    file = FileField(
        validators=[DataRequired(), FileAllowed(['pdf', 'doc', 'docx'], message="请上传pdf, doc, docx格式文件")])
    description = TextAreaField(
        default=''
    )
    required_points = IntegerField(
        default=0,
        validators=[]
    )
    # def validate_file_type(self, value):
    #     try:
    #         file_client = FileFormatEnum(value.data)
    #     except ValueError as e:
    #         raise ValidationError(f'{e}')
